package com.whfc.emp.dpa.queue;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.third.map.MapApi;
import com.whfc.common.third.map.MapLoc;
import com.whfc.common.util.Gps;
import com.whfc.emp.cncit.entity.CncitUpMsgS;
import com.whfc.emp.entity.AppDeviceCardLog;
import com.whfc.emp.enums.PosState;
import com.whfc.emp.manager.AppEmpDataManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/12/2 11:59
 */
@Component
@RabbitListener(queues = QueueConst.EMP_HELMET_DATA)
public class HelmetDataQueueMessageListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppEmpDataManager appEmpDataManager;

    @Autowired
    private MapApi mapApi;

    @RabbitHandler
    public void process(String msg) {
        try {
            logger.info("helmet|data|{}", msg);
            CncitUpMsgS reqMsg = JSONObject.parseObject(msg, CncitUpMsgS.class);
//            1、删除坐标转换代码：移除mapApi.translateToGcj02()调用
//            2、直接使用原始坐标：直接使用reqMsg.getLatWgs84()和reqMsg.getLngWgs84()作为最终坐标
//            3、保持其他逻辑不变：地址解析、数据保存等逻辑保持不变
            //坐标系转换
//            Gps gcj02Gps = mapApi.translateToGcj02(reqMsg.getLngWgs84(), reqMsg.getLatWgs84());
//            Double lat = gcj02Gps.getLat();
//            Double lng = gcj02Gps.getLng();

            AppDeviceCardLog cardLog = new AppDeviceCardLog();
            BeanUtils.copyProperties(reqMsg, cardLog);
//            cardLog.setLng(lng);
//            cardLog.setLat(lat);
//            MapLoc loc = mapApi.geocode(lng, lat);
            Double lat = reqMsg.getLatWgs84();  // 直接使用原始纬度
            Double lng = reqMsg.getLngWgs84();  // 直接使用原始经度
            cardLog.setLng(lng);
            cardLog.setLat(lat);
            MapLoc loc = mapApi.geocode(lng, lat);
            cardLog.setLocation(loc.getAddress());
            cardLog.setPosState(PosState.VALID.getValue());
            appEmpDataManager.addHelmetData(cardLog);
        } catch (Exception ex) {
            logger.error("helmet|data,消息处理失败", ex);
        }
    }
}
